package com.spic.business.staff.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.spic.business.train.trainCertificate.domain.TrainCertificate;
import com.spic.business.train.trainInfo.domain.TrainInfo;
import com.spic.business.train.trainInfo.dto.TrainListDto;
import com.spic.business.train.trainInfo.mapper.TrainInfoMapper;
import com.spic.business.violation.supervision.action.domain.ViolationActionInput;
import com.spic.business.violation.supervision.action.service.IViolationActionInputService;
import com.spic.business.violation.supervision.information.domain.ViolationInformation;
import com.spic.business.violation.supervision.information.service.IViolationInformationService;
import com.spic.common.core.constant.SecurityConstants;
import com.spic.common.core.domain.R;
import com.spic.common.core.exception.ServiceException;
import com.spic.common.core.utils.DateUtils;
import com.spic.common.core.utils.ServletUtils;
import com.spic.common.core.utils.StringUtils;
import com.spic.system.api.RemoteUserService;
import com.spic.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.spic.business.staff.mapper.StaffBasicDataMapper;
import com.spic.business.staff.domain.StaffBasicData;
import com.spic.business.staff.service.IStaffBasicDataService;

/**
 * 人员基本资料Service业务层处理
 *
 * @author ruoyi
 * @date 2023-03-23
 */
@Service
public class StaffBasicDataServiceImpl implements IStaffBasicDataService
{

    private static final String trainingSystemUrl = "https://sd.sdmkpx.com:10007/quanyuanApi/blade-quanyuan-exam/external/certificatesapi/unAuth/getCertificatesList";

    @Autowired
    private StaffBasicDataMapper staffBasicDataMapper;
    @Autowired
    private IViolationInformationService violationInformationService;
    @Autowired
    private IViolationActionInputService violationActionInputService;
    @Autowired
    private RemoteUserService remoteUserService;
    @Autowired
    private TrainInfoMapper trainInfoMapper;

    /**
     * 查询人员基本资料
     *
     * @param staffBasicData 人员基本资料
     * @return 人员基本资料
     */
    @Override
    public List<StaffBasicData> selectStaffBasicDataByUserName(StaffBasicData staffBasicData)
    {
        return staffBasicDataMapper.selectStaffBasicDataByUserName(staffBasicData);
    }

    /**
     * 查询人员基本资料列表
     *
     * @param staffBasicData 人员基本资料
     * @return 人员基本资料
     */
    @Override
    public List<StaffBasicData> selectStaffBasicDataList(StaffBasicData staffBasicData)
    {
        return staffBasicDataMapper.selectStaffBasicDataList(staffBasicData);
    }

    /**
     * 新增人员基本资料
     *
     * @param staffBasicData 人员基本资料
     * @return 结果
     */
    @Override
    public int insertStaffBasicData(StaffBasicData staffBasicData)
    {
        staffBasicData.setCreateTime(DateUtils.getNowDate());
        staffBasicData.setCreateBy(ServletUtils.getRequest().getHeader(SecurityConstants.DETAILS_USER_ID));
        return staffBasicDataMapper.insertStaffBasicData(staffBasicData);
    }

    /**
     * 修改人员基本资料
     *
     * @param staffBasicData 人员基本资料
     * @return 结果
     */
    @Override
    public int updateStaffBasicData(StaffBasicData staffBasicData)
    {
        return staffBasicDataMapper.updateStaffBasicData(staffBasicData);
    }

    /**
     * 批量删除人员基本资料
     *
     * @param ids 需要删除的人员基本资料主键
     * @return 结果
     */
    @Override
    public int deleteStaffBasicDataByIds(Long[] ids)
    {
        return staffBasicDataMapper.deleteStaffBasicDataByIds(ids);
    }

    /**
     * 删除人员基本资料信息
     *
     * @param id 人员基本资料主键
     * @return 结果
     */
    @Override
    public int deleteStaffBasicDataById(Long id)
    {
        return staffBasicDataMapper.deleteStaffBasicDataById(id);
    }

    @Override
    public List<TrainCertificate> getCertificatesList(String identityCard) {
        if(StringUtils.isEmpty(identityCard)) {
            throw new ServiceException("身份证号不能为空");
        }
        List<TrainCertificate> data= new ArrayList<>();
        String[] array = {identityCard};
        JSONObject paramMap = new JSONObject();
        paramMap.put("tenantId", "00006");
        paramMap.put("idCards", array);
        String jsonStr = HttpUtil.post(trainingSystemUrl, paramMap.toJSONString(), 5000);
        JSONObject resultJson = JSONObject.parseObject(jsonStr);
        if (resultJson.getInteger("code") == 200) {
            data = JSON.parseArray(resultJson.getString("data"), TrainCertificate.class);
        }
        return data;
    }

    @Override
    public List<Map<String, Object>> getViolationList(String personnelCode) {
        List<Map<String, Object>> list = new ArrayList<>();
        R<LoginUser> userInfo = remoteUserService.getUserInfo(personnelCode, SecurityConstants.INNER);
        if(userInfo.getCode() != 200) {
            throw new ServiceException("获取用户信息失败");
        }
        ViolationInformation violationInformation = new ViolationInformation();
        violationInformation.setResponsible(String.valueOf(userInfo.getData().getSysUser().getUserId()));
        List<ViolationInformation> informations = violationInformationService.selectViolationInformationListWithOutDataPermission(violationInformation);

        ViolationActionInput violationActionInput = new ViolationActionInput();
        violationActionInput.setIllegalMan(String.valueOf(userInfo.getData().getSysUser().getUserId()));
        List<ViolationActionInput> violationInputs = violationActionInputService.selectViolationActionInputListWithOutDataPermission(violationActionInput);
        for (int i = 0; i < informations.size(); i++) {
            ViolationInformation violationInfo = informations.get(i);
            Map<String, Object> map = new HashMap<>();
            map.put("illegalTime", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, violationInfo.getIllegalTime()));
            map.put("illegalDesc", violationInfo.getIllegalInstructions());
            map.put("illegalImage", violationInfo.getIllegalImagesBefore());
            map.put("status", violationInfo.getStatus());
            map.put("deduction", violationInfo.getDeduction());
            map.put("penaltyAmount", violationInfo.getPenaltyAmount());
            list.add(map);
        }
        for (int i = 0; i < violationInputs.size(); i++) {
            ViolationActionInput input = violationInputs.get(i);
            Map<String, Object> map = new HashMap<>();
            map.put("illegalTime", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, input.getIllegalTime()));
            map.put("illegalDesc", input.getIllegalDesc());
            map.put("illegalImage", input.getIllegalImages());
            map.put("status", input.getStatus());
            map.put("deduction", input.getDeduction());
            map.put("penaltyAmount", input.getPunishAmount());
            list.add(map);
        }
        return list;
    }

    @Override
    public List<TrainInfo> getTrainInfo(String personnelCode) {
        R<LoginUser> userInfo = remoteUserService.getUserInfo(personnelCode, SecurityConstants.INNER);
        if(userInfo.getCode() != 200) {
            throw new ServiceException("获取用户信息失败");
        }
        TrainListDto trainListDto = new TrainListDto();
        trainListDto.setUserId(userInfo.getData().getSysUser().getUserId());
        List<TrainInfo> trainInfos=trainInfoMapper.selectTrainInfoListByUserId(trainListDto);
        return trainInfos;
    }
}
